try, catch
执行语句并捕获产生的错误
语法
trystatements
catchexception
statements
end
说明
try
执行 statements
, catch statements
endtry
块中的语句并在 catch
块中捕获产生的错误。此方法允许您改写一组程序语句的默认错误行为。如果 try
块中的任何语句生成错误,程序控制将立即转至包含错误处理语句的 catch
块。
exception
是 MException
对象,您可以用它来标识错误。catch
块将当前异常对象分配给 exception
中的变量。
try
和 catch
块都可包含嵌套的 try/catch
语句。
示例
补充错误消息
创建两个无法垂直串联的矩阵。
A = rand(3); B = ones(5); C = [A; B];
Error using vertcat Dimensions of matrices being concatenated are not consistent.
使用 try/catch
显示有关维度的详细信息。
try C = [A; B]; catch ME if (strcmp(ME.identifier,'MATLAB:catenate:dimensionMismatch')) msg = ['Dimension mismatch occurred: First argument has ', ... num2str(size(A,2)),' columns while second has ', ... num2str(size(B,2)),' columns.']; causeException = MException('MATLAB:myCode:dimensions',msg); ME = addCause(ME,causeException); end rethrow(ME) end
Error using vertcat Dimensions of matrices being concatenated are not consistent. Caused by: Dimension mismatch occurred: First argument has 3 columns while second has 5 columns.
如果矩阵维度不一致,则 MATLAB® 将显示有关不匹配的更多信息。而所有其他错误也都将正常显示。
将错误重新打包为警告
捕获通过调用不存在的函数 notaFunction
而生成的任何异常。如果存在异常,则发出警告并为输出分配值 0。
try a = notaFunction(5,6); catch warning('Problem using function. Assigning a value of 0.'); a = 0; end
Warning: Problem using function. Assigning a value of 0.
对 notaFunction
的调用本身会导致错误。如果使用 try
和 catch
,此代码将捕获任何异常并将其重新打包为警告,这样 MATLAB 就可以继续执行后续命令。
处理不同类型的错误
使用 try/catch
以不同的方式处理不同类型的错误。
如果未定义函数
notaFunction
,将会发出警告而不是错误,并为输出赋值NaN
。如果
notaFunction.m
存在,但是为脚本而不是函数,将会发出警告而不是错误,运行该脚本,并为输出分配值0
。如果 MATLAB 由于任何其他原因而引发错误,将重新引发该异常。
try a = notaFunction(5,6); catch ME switch ME.identifier case 'MATLAB:UndefinedFunction' warning('Function is undefined. Assigning a value of NaN.'); a = NaN; case 'MATLAB:scriptNotAFunction' warning(['Attempting to execute script as function. '... 'Running script and assigning output a value of 0.']); notaFunction; a = 0; otherwise rethrow(ME) end end
Warning: Function is undefined. Assigning a value of NaN.
提示
您不能在一个
try
块中使用多个catch
块,但可以嵌套完整的try/catch
块。与一些其他语言不同,MATLAB 不允许在
try/catch
语句中使用finally
块。
扩展功能
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出R2022a: 改进了语句无错误运行时的性能
try
模块显示在该模块中的语句无错误运行时性能得到了改进。例如,以下代码比上一版本大约快 6 倍。
function testTryPerformance x = 1; for i = 1:1e8 try x = x * i; catch warning("Assignment was not successful.") x = 1; end end end
大致的执行时间是:
R2021b:2.3 秒
R2022a:0.4 秒
代码是在运行 Windows® 10 的 Intel® Xeon® CPU E5-1650 v4 @ 3.60 GHz 测试系统上使用 timeit
函数进行计时的。
timeit(@testTryPerformance)
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)